package mobile.map;

import java.util.ArrayList;
import java.util.List;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.OverlayItem;


import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;

public class FHMinLocationListener implements LocationListener{
	String lastLocation = null;
	private String bestGuess = "No current close room!";
	MapActivity localMA;
	List<GeoPoint> localGP = new ArrayList<GeoPoint>();
	List<OverlayItem> localOI = new ArrayList<OverlayItem>();
	public FHMinLocationListener(MapActivity myMA, List<GeoPoint> myGP, List<OverlayItem> myOI) {
		localMA = myMA;
		localGP = myGP;
		localOI = myOI;
	}

	public FHMinLocationListener() {
		localMA = null;
		localGP = null;
		localOI = null;
	}

	public void onLocationChanged(Location location) {
		float currentBestDistance = (float) 1E6;
		
		for (int i = 0; i < localGP.size(); i++) {
			float[] results = new float[3];
			Location.distanceBetween(location.getLatitude(), location.getLongitude(),
					localGP.get(i).getLatitudeE6() / (double) 1E6, localGP.get(i).getLongitudeE6() / (double) 1E6, results);

			if (results[0] < currentBestDistance) { 
				bestGuess = localOI.get(i).getTitle();
				currentBestDistance = results[0];
			}
		}
		if ((currentBestDistance < 1000) && (lastLocation != bestGuess)) { //gotta have some sort of threshold
			FHMinToast.ShowText(localMA, bestGuess);
			lastLocation = bestGuess;
		}
	}

	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub

	}

	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub

	}

	public void onStatusChanged(String provider, int status,
			Bundle extras) {
		// TODO Auto-generated method stub

	}
	
	public void announceLocation() {
		FHMinToast.ShowText(localMA, bestGuess);
	}

}
